package org.test.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class PoiCustomComm {

	private static Logger log = Logger.getLogger(PoiCustomComm.class);

	/**
	 * 
	 * genExcel
	 * 
	 * @param head
	 *            头
	 * @param content
	 *            内容
	 * @Exception
	 * @author：denggm@asiainfo-linkage.com
	 * @Oct 10, 2012 3:16:33 PM
	 * @update:
	 * @Oct 10, 2012 3:16:33 PM
	 */
	public static HSSFWorkbook getExcelWorkBook(List head, List allContentList)
			throws Exception {

		HSSFWorkbook workbook = new HSSFWorkbook();
		// create a new sheet
		HSSFSheet sheet = workbook.createSheet("导出工作表");

		// Define a few rows

		// 填充头
		HSSFRow headRow = sheet.createRow(0);
		for (int j = 0; j < head.size(); j++) { // 填列
			String headColumn = (String) head.get(j);
			HSSFCell headCell = headRow.createCell(j);
			headCell.setCellValue(headColumn);
		}

		// 填充content
		for (int j = 0; j < allContentList.size(); j++) { // 填行
			HSSFRow contentRow = sheet.createRow(j + 1);
			List contentRowList = (List) allContentList.get(j); // 单行的内容
			for (int k = 0; k < contentRowList.size(); k++) { // 填列
				String contentColumn = (String) contentRowList.get(k);
				HSSFCell contentCell = contentRow.createCell(k);
				contentCell.setCellValue(contentColumn);
			}
		}

		// Save

		return workbook;
	}

	/**
	 * 生成execl表,内容现在只支持String
	 * 
	 * @param fileName
	 * @param head
	 * @param allContentList
	 * @throws Exception
	 *             void
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 11, 2012 9:15:41 PM
	 */
	public static void generateExcel(String fileName, List head,
			List allContentList) throws Exception {
		HSSFWorkbook workbook = getExcelWorkBook(head, allContentList);
		File file = new File(fileName);
		FileOutputStream out = new FileOutputStream(file);
		workbook.write(out);
		out.close();
	}

	/**
	 * 读取Excel
	 * 
	 * @param fileName
	 *            void
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 11, 2012 9:21:53 PM
	 */
	public static List readExcel(String fileName) throws Exception {
		InputStream inp = new FileInputStream(fileName);
		HSSFWorkbook workbook = new HSSFWorkbook(inp);

		HSSFSheet sheet = workbook.getSheetAt(0);
		
		List<List> rowList = new ArrayList<List>();

		// 最大行
		int maxRowNum = sheet.getLastRowNum();
		log.debug("最大行数为：" + maxRowNum);

		for (int i = 0; i <= maxRowNum; i++) {
			List<String> columnList = new ArrayList<String>();
			
			HSSFRow currentRow = sheet.getRow(i);
			int maxColumnNum = currentRow.getLastCellNum();
		//	log.debug("第" + i + "行的列数为" + maxColumnNum);
			for (int j = 0; j < maxColumnNum; j++) {
				Cell currentCell = currentRow.getCell(j);
				String currentCellVal = "";
				if (currentCell != null) {
					currentCellVal = currentCell.getStringCellValue();
				}
				columnList.add(currentCellVal);
		//		log.debug(currentCellVal);
			}
			rowList.add(columnList);
		}
		return rowList;
	}


}
